from . import main
from flask import render_template,request,current_app
from app.models import Article,Category,User,Setting
#上下文全局变量
@main.context_processor
def make_template_context():
    setting = Setting.query.first()
    category_main = Category.query.all()
    user = User.query.first()
    pagination = Article.query.order_by(
        Article.id.desc()).paginate(
        page=int(request.args.get('page',1)),per_page=4,
        error_out=False)
    return dict(setting=setting,category_main=category_main,
    user=user,articles=pagination.items,
    pagination=pagination)
#路由定义
@main.route('/favicon.ico')
def favicon():
    return current_app.send_static_file('favicon.ico')
@main.route('/')
def index():
    return render_template('main/index.html')
@main.route('/category/<name>/')
def category(name):
    category=Category.query.filter_by(name=name).first()
    pagination = Article.query.filter_by(category_id=category.id).order_by(Article.id.desc()).paginate(
        page=int(request.args.get('page',1)),per_page=4,
        error_out=False)
    return render_template('main/category.html',articles=pagination.items,pagination=pagination,category=category)
@main.route('/about/')
def about():
    return render_template('main/about.html')
@main.route('/article/<int:id>.html')
def article(id):
    article=Article.query.get_or_404(id)
    return render_template('main/article.html',article=article)